﻿using System;
using System.Linq;
using System.Windows.Forms;
using NanoERP.Infrastructure;
using NanoERP.Business.BaseInfo;
using NanoERP.Model;
using System.Collections.Generic;
using NanoERP.Common;

namespace NanoERP.BaseInfo
{
    //[Module("FrmSelectAccount")]
    public partial class FrmSelectAccount : ModuleForm
    {
        readonly AccountinfoBLL _accountinfobll;
        readonly Dictionary<long, string> _dicSelected;
        readonly string _accountInfo;

        public FrmSelectAccount()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选择账户
        /// </summary>
        /// <param name="accountInfo">账户信息</param>
        public FrmSelectAccount(string accountInfo)
        {
            InitializeComponent();

            _accountinfobll = new AccountinfoBLL();
            _dicSelected = new Dictionary<long, string>();
            _accountInfo = accountInfo;

            this.Load += FrmSelectAccount_Load;

        }

        void FrmSelectAccount_Load(object sender, EventArgs e)
        {
            var strArrs = new List<string[]>();
            if (_accountInfo.Length > 0)
            {
                strArrs = (from g in _accountInfo.Split(';')
                           select g.Split(' ')).ToList();
            }

            dgvList.AutoGenerateColumns = false;
            _accountinfobll.Query().ForEach(r => dgvList.Invoke(new MethodInvoker(() =>
                {
                    var v = strArrs.FirstOrDefault(s => s[0].Equals(r.Accountname));
                    var index = dgvList.Rows.Add(r.Id, r.Accountname, v != null ? v[1] : "0");

                    if (v != null) _dicSelected.Add(r.Id, string.Format("{0} {1}", r.Accountname, v[1]));
                    dgvList.Rows[index].Tag = r;
                })));
        }

        private void dgvList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            var rectangle = new System.Drawing.Rectangle(e.RowBounds.Location.X,
                  e.RowBounds.Location.Y,
                  dgvList.RowHeadersWidth - 4,
                  e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                dgvList.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dgvList.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            this.Tag = _dicSelected;
            this.DialogResult = DialogResult.OK;
        }

        private void dgvList_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || dgvList.CurrentRow == null) return;
            if (e.ColumnIndex == 2)
            {
                var vv = dgvList.CurrentRow.Tag as Accountinfo;
                var cell = Normal.ToInt(dgvList.CurrentCell.Value);
                if (cell > 0)
                {
                    if (!_dicSelected.ContainsKey(vv.Id))
                        _dicSelected.Add(vv.Id, string.Format("{0} {1}", vv.Accountname, cell));
                    else
                        _dicSelected[vv.Id] = string.Format("{0} {1}", vv.Accountname, cell);
                }
                else
                {
                    _dicSelected.Remove(vv.Id);
                }
            }
        }
    }
}
